package com.sprouting.ops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.OpsComputerFile;
import com.sprouting.ops.model.OpsComputerServer;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsComputerFileQo;
import com.sprouting.ops.service.OpsComputerFileService;
import com.sprouting.ops.service.OpsComputerServerService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 计算机关键文件与文件夹(OpsComputerFile)() 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-04-23 21:23:51
 */
@Slf4j
@RestController
@RequestMapping("opsComputerFile")
public class OpsComputerFileController {

    @Resource
    private OpsComputerFileService opsComputerFileService;
    @Resource
    private OpsComputerServerService opsComputerServerService;


    /**
     * 分页查询计算机关键文件与文件夹
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(OpsComputerFileQo query){
        Page page = query.getPage();
        return ResultJson.success(opsComputerFileService.getListPage(page, query));
    }

    /**
     * 删除计算机关键文件与文件夹
     * @param id 计算机关键文件与文件夹id
     * @return
     */
    @PostMapping("delOpsComputerFile")
    @IdempotentSingle
    public ResultJson delOpsComputerFile(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        opsComputerFileService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 更新计算机关键文件与文件夹
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsComputerFile")
    @IdempotentSingle
    public ResultJson updOpsComputerFile(@Validated(UpdGroup.class) OpsComputerFile bo){
        String msg = checkOpsComputerFile(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        bo.setSiteId(null);
        EntityTool.updBase(bo);
        opsComputerFileService.updateById(bo);
        return ResultJson.success(bo);
    }

    /**
     * 保存计算机关键文件与文件夹
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsComputerFile")
    @IdempotentSingle
    public ResultJson saveOpsComputerFile(@Validated(AddGroup.class) OpsComputerFile bo){
        String msg = checkOpsComputerFile(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }
        OpsComputerServer server = opsComputerServerService.getById(bo.getComputerServerId());
        if (server == null){
            return ResultJson.errorCheck();
        }

        bo.setSiteId(server.getId());
        bo.setId(IdTool.getId());
        EntityTool.saveBase(bo);
        opsComputerFileService.save(bo);
        return ResultJson.success(bo);
    }


    /**
     * 计算机关键文件与文件夹数据校验
     * @param bo 计算机关键文件与文件夹 校验数据
     * @return 非null则有错误
     */
    private String checkOpsComputerFile(OpsComputerFile bo){

        // 补充数据
        if (bo.getCore() == null){
            bo.setCore(ConstantBase.ComputerFileCore.LOW);
        }

        return null;
    }

}
